package com.ldb.sharedpreferencesstudy;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import java.util.HashMap;
import java.util.Map;

public class Utils {
    /*保存QQ号码和登录密码,到mydata.xml文件中*/
    public static boolean saveUserInfo(Context context,String num,String password){
        SharedPreferences sharedPreferences = context.getSharedPreferences("mydata",Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit(); //获取编辑器
        editor.putString("userName",num); //保存号码
        editor.putString("password",password); //保存密码
        return editor.commit(); //提交修改
    }

    /*从mydata.xml文件中获取存储的QQ号码和密码*/
    public static Map<String, String> getUserInfo(Context context){
        SharedPreferences sharedPreferences = context.getSharedPreferences("mydata",Context.MODE_PRIVATE);

        String number = sharedPreferences.getString("userName",null);//获取号码
        String password = sharedPreferences.getString("password",null);//获取密码

        if (TextUtils.isEmpty(number)||TextUtils.isEmpty(password)){
            /*账号或者密码为空时，返回null*/
            return null;
        }else {
            /*账号和密码均不为空，说明用户以前登录成功后的账号和密码均已保存*/
            Map<String,String> userMap = new HashMap<String,String>();
            userMap.put("userName",number);
            userMap.put("password",password);
            return userMap;
        }
    }
}
